import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Train {
    public static void main1(String[] args) throws SQLException {
        DataSource dataSource =new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test1?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("111111");

        dataSource.getConnection();
        Connection connection=dataSource.getConnection();

        String sql="insert into student values(6,'老李')";
        PreparedStatement statement = connection.prepareStatement(sql);

        statement.executeUpdate();

        statement.close();
        connection.close();
    }

    public static void main11(String[] args) throws SQLException {
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入id: ");
        int id=scanner.nextInt();
        System.out.println("请输入name: ");
        String name=scanner.next();

        DataSource dataSource =new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test1?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("111111");

        dataSource.getConnection();
        Connection connection=dataSource.getConnection();

        String sql="insert into student values(?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,id);
        statement.setString(2,name);

        statement.executeUpdate();

        statement.close();
        connection.close();
    }

    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test1?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("111111");

        dataSource.getConnection();
        Connection connection=dataSource.getConnection();

        String sql="select * from student;";
        PreparedStatement statement= connection.prepareStatement(sql);

        ResultSet resultSet=statement.executeQuery();

        while(resultSet.next()){
            System.out.println(resultSet.getInt("id"));;
            System.out.println(resultSet.getString("name"));;
        }
        resultSet.close();
        statement.close();
        connection.close();
    }
}
